//
//  WCNCareRecordDetailVC.m
//  WCN
//
//  Created by 杨鑫 on 15/6/18.
//  Copyright (c) 2015年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

#import "WCNCareRecordDetailVC.h"
//
#import "WCNMulTextTCell.h"
#import "WCNCareRecordEditTCell.h"
#import "WCNCareRecordItemTCell.h"
#import "WCButton.h"
#import "WCDrawBorderView.h"
#import "WCNCareRecordDetailFooterView.h"
#import "WCImgBrowserView.h"
//
#import "UILabel+Utilities.h"
//
#import "WCImageManager.h"
//
#import "WSJHorTableView.h"
#import "UINavigationItem+Offset.h"

static NSString *cellId = @"WCNMulTextTCell";
static NSString *cellId2 = @"WCNCareRecordEditTCell";
static NSString *cellId3 = @"WCNCareRecordItemTCell";

@interface WCNCareRecordDetailVC ()<UITableViewDataSource, UITableViewDelegate, WCImgBrowserViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic) WCImageType imageType;
@property (strong, nonatomic) NSMutableDictionary *data;

@property (strong, nonatomic) NSMutableArray *drugs;
@property (strong, nonatomic) NSMutableArray *deals;

@property (nonatomic, strong) WCNMulTextTCell *prototypeCell;
@property (nonatomic, strong) WCNCareRecordItemTCell *prototypeCell2;

@property (strong, nonatomic) NSMutableArray *imageViews;
@property (weak, nonatomic) UIImageView *showView;

@end

@implementation WCNCareRecordDetailVC

- (instancetype)initWith:(NSDictionary *)data
{
    self = [super init];
    if (self) {
        self.title = @"伤口护理记录";
        
        _data = [NSMutableDictionary dictionaryWithDictionary:data];
        _drugs = data[@"medicines"];
        _deals = data[@"treatments"];
        
        _imageType = WCImageTypePrivateImage;
        _imageViews = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    WCDrawBorderView *tableHeaderView = [[WCDrawBorderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 15)];
    tableHeaderView.borderColor = kBorderColor;
    tableHeaderView.bottomBorder = YES;
    tableHeaderView.backgroundColor = [UIColor clearColor];
    self.tableView.tableHeaderView = tableHeaderView;
    
    [self.tableView registerNib:[UINib nibWithNibName:cellId bundle:nil] forCellReuseIdentifier:cellId];
    [self.tableView registerNib:[UINib nibWithNibName:cellId2 bundle:nil] forCellReuseIdentifier:cellId2];
    [self.tableView registerNib:[UINib nibWithNibName:cellId3 bundle:nil] forCellReuseIdentifier:cellId3];
    
    self.prototypeCell = [self.tableView dequeueReusableCellWithIdentifier:cellId];
    [self.prototypeCell.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.prototypeCell.contentView
                                                                               attribute:NSLayoutAttributeWidth
                                                                               relatedBy:NSLayoutRelationEqual
                                                                                  toItem:nil
                                                                               attribute:NSLayoutAttributeNotAnAttribute
                                                                              multiplier:1
                                                                                constant:kScreenWidth]];
    self.prototypeCell.mulText.numberOfLines = 0;
    
    self.prototypeCell2 = [self.tableView dequeueReusableCellWithIdentifier:cellId3];
    [self.prototypeCell2.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.prototypeCell2.contentView
                                                                               attribute:NSLayoutAttributeWidth
                                                                               relatedBy:NSLayoutRelationEqual
                                                                                  toItem:nil
                                                                               attribute:NSLayoutAttributeNotAnAttribute
                                                                              multiplier:1
                                                                                constant:kScreenWidth]];
    self.prototypeCell2.itemTitle.numberOfLines = 0;
    
    NSArray *images = self.data[@"images"];
    if (images.count > 0) {
        UIView *tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 70+98+98+34)];
        [self loadFooterView:tableFooterView];
        self.tableView.tableFooterView = tableFooterView;
    } else {
        self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    }
    
}

#pragma mark - 加载底部图片编辑
- (void)loadFooterView:(UIView *)tableFooterView
{
    WCDrawBorderView *view1 = [[WCDrawBorderView alloc] initWithFrame:CGRectMake(0, -kOnePX, kScreenWidth, 45+kOnePX)];
    view1.topBorder = YES;
    view1.borderColor = kBorderColor;
    view1.backgroundColor = [UIColor whiteColor];
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(14, 10, 200, 25)];
    label1.font = [UIFont systemFontOfSize:16.0];
    label1.text = @"伤口图片";
    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWidth-11-13, 16, 13, 13)];
    imageView1.image = [UIImage imageNamed:@"Arrow_Down"];
    [view1 addSubview:label1];
    [view1 addSubview:imageView1];
    
    WCNCareRecordDetailFooterView *footerView = [[WCNCareRecordDetailFooterView alloc] initWithFrame:CGRectMake(0, 40, kScreenWidth, 90)];
//    footerView.backgroundColor = [UIColor greenColor];
    NSArray *images = self.data[@"images"];
    WSJHorTableView *picTableView =[[WSJHorTableView alloc] initWithFrame:CGRectMake(0, 10, kScreenWidth, 65)];
    picTableView.type = 1;
    picTableView.imageUrls =images;
    picTableView.presentDelegate = ^(){return self;};
    [footerView addSubview:picTableView];

    [tableFooterView addSubview:view1];
    [tableFooterView addSubview:footerView];
}

-(void)dealloc{
    NSLog(@"---------WCNCareRecordDetailVC----------------");
}

#pragma mark - Setter

- (void)setImagesDir:(NSInteger)integer
{
    self.imageType = integer;
}

#pragma mark - <UITableViewDataSource, UITableViewDelegate>

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *tCell;
    if (indexPath.section == 0) {
        WCNMulTextTCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
        
        cell.mulText.textAlignment = NSTextAlignmentRight;
        cell.mulText.numberOfLines = 1;
        [cell.arowImg removeFromSuperview];
        
        NSInteger row = indexPath.row;
        if (!self.data[@"bodyPartName"]) {
            row++;
        }
        
        switch (row) {
            case 0: {
                cell.textTitle.text = @"伤口部位";
                cell.mulText.text = self.data[@"bodyPartName"];
                break;
            }
            case 1: {
                cell.textTitle.text = @"伤口天数";
                if (self.data[@"targetDay"]) {
                    cell.mulText.text = [NSString stringWithFormat:@"Day%@", self.data[@"targetDay"]];
                } else {
                    cell.mulText.text = @"";
                }
                break;
            }
            case 2: {
                cell.textTitle.text = @"伤口描述";
                cell.mulText.textAlignment = NSTextAlignmentLeft;
                cell.mulText.numberOfLines = 0;
                cell.mulText.text = self.data[@"description"];
                [cell.mulText showAttributedText:0 lineSpace:5];
                break;
            }
        }
        
        tCell = cell;
    } else if (indexPath.section == 1 || indexPath.section == 2) {
        if (indexPath.row == 0) {
            WCNCareRecordEditTCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId2 forIndexPath:indexPath];
            [cell.addButton removeFromSuperview];
            [cell.deleteButton removeFromSuperview];
            
            if (indexPath.section == 1) {
                cell.headerTitle.text = @"使用药品";
            } else {
                cell.headerTitle.text = @"处理方案";
            }
            
            tCell = cell;
        } else {
            WCNCareRecordItemTCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId3 forIndexPath:indexPath];
            [cell.editIcon removeFromSuperview];
            cell.itemTitle.numberOfLines = 0;
            if (indexPath.section == 1) {
                cell.itemTitle.text = self.drugs[indexPath.row-1][@"name"];
            } else {
                cell.itemTitle.text = self.deals[indexPath.row-1][@"name"];
            }
            
            tCell = cell;
        }
    } else if (indexPath.section == 3) {
        if (indexPath.row == 0) {
            UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.backgroundColor = HexRGB(0xf8f8f8);
            
            tCell = cell;
        } else {
            WCNMulTextTCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
            cell.mulText.textAlignment = NSTextAlignmentRight;
            [cell.arowImg removeFromSuperview];
            cell.textTitle.text = @"备注";
            cell.mulText.textAlignment = NSTextAlignmentLeft;
            cell.mulText.numberOfLines = 0;
            cell.mulText.text = self.data[@"note"];
            [cell.mulText showAttributedText:0 lineSpace:5];
            
            tCell = cell;
        }
    }
    
    [tCell setSeparatorInset:UIEdgeInsetsZero];
    if ([tCell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [tCell setPreservesSuperviewLayoutMargins:NO];
    }
    if ([tCell respondsToSelector:@selector(setLayoutMargins:)]) {
        [tCell setLayoutMargins:UIEdgeInsetsZero];
    }
    
    return tCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        NSInteger row = indexPath.row;
        if (!self.data[@"bodyPartName"]) {
            row++;
        }
        if (row == 2) {
            self.prototypeCell.mulText.text = self.data[@"description"];
            [self.prototypeCell.mulText showAttributedText:0 lineSpace:5];
            
            CGSize size = [self.prototypeCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
            return MAX(kOnePX+size.height, 45.0);
        }
    } else if (indexPath.section == 1 || indexPath.section == 2) {
        if (indexPath.row == 0) {
            return 30;
        } else {
            if (indexPath.section == 1) {
                self.prototypeCell2.itemTitle.text = self.drugs[indexPath.row-1][@"name"];
            } else {
                self.prototypeCell2.itemTitle.text = self.deals[indexPath.row-1][@"name"];
            }
            [self.prototypeCell2.itemTitle showAttributedText:0 lineSpace:5];
            
            CGSize size = [self.prototypeCell2.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
            return MAX(kOnePX+size.height, 45.0);
        }
    } else if (indexPath.section == 3) {
        if (indexPath.row == 0) {
            return 15;
        } else {
            self.prototypeCell.mulText.text = self.data[@"note"];
            [self.prototypeCell.mulText showAttributedText:0 lineSpace:5];
            
            CGSize size = [self.prototypeCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
            return MAX(kOnePX+size.height, 45.0);
        }
    }
    
    return 45;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        if (self.data[@"bodyPartName"]) {
            return 3;
        }
        return 2;
    } else if (section == 1) {
        return 1+self.drugs.count;
    } else if (section == 2) {
        return 1+self.deals.count;
    } else if (section == 3) {
        return 2;
    }
    return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}

#pragma mark - WCImgBrowserViewDelegate

- (UIImageView *)viewOfBrowserViewshowFrom
{
    return self.showView;
}

- (UIImageView *)viewOfBrowserViewRemoveToAtIndex:(NSInteger)index
{
    return self.imageViews[index];
}

- (NSInteger)numberOfImagesInBrowserView
{
    NSArray *images = self.data[@"images"];
    return images.count;
}

- (NSString *)browserItemIdForItemAtIndex:(NSInteger)index
{
    NSArray *images = self.data[@"images"];
    return images[index];
}

- (WCImageType)browserItemTypeForItemAtIndex:(NSInteger)index
{
    return self.imageType;
}

- (NSInteger)indexForFirstShow
{
    return self.showView.tag;
}

@end
